package com.yjq.test.gateway;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包含网关和排他网关的联合使用实现请假的工作流程定义
 */
public class GateWay02 {
    @Test
    public void deployProcess(){
        //获取流程引擎
        ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
        //获取repositoryService
        RepositoryService repositoryService=processEngine.getRepositoryService();
        //将流程定义的bpmn文件存入数据库
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("bpmn/evection02.bpmn")
                .addClasspathResource("bpmn/evection02.png")
                .name("包含网关测试员工请假申请流程")
                .deploy();

        //输出部署之后的流程信息
        System.out.println("流程部署的id:"+deploy.getId());
        System.out.println("流程定义的关键字:"+deploy.getKey());
        System.out.println("流程名称："+deploy.getName());
    }



    @Test
    public void createInstance(){
        //获取流程引擎
        ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
        //获取RuntimeService
        RuntimeService runtimeService=processEngine.getRuntimeService();
        //定义流程变量
        Map map=new HashMap();
        map.put("day",5);
        //创建流程实例
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("evection02",map);

        //输出流程实例信息
        System.out.println("流程实例id:"+instance.getId()+"  "+instance.getProcessInstanceId());
        System.out.println("流程定义id："+instance.getProcessDefinitionId());
        System.out.println("流程部署id："+instance.getDeploymentId());
    }



    @Test
    public void complete(){
        //获取流程引擎
        ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
        //获取TaskService;
        TaskService taskService=processEngine.getTaskService();
        //责任人
        String assignee="张三";
        //根据任务责任人和流程实例id查询该员工的待执行的任务
        Task task = taskService.createTaskQuery()
                .processInstanceId("185001")
                .taskAssignee(assignee)
                .singleResult();
        taskService.complete(task.getId());
        System.out.println("当前完成的任务:"+task.getName());
        System.out.println("责任人:"+task.getAssignee());
        System.out.println("实例id："+task.getProcessInstanceId());
    }



    @Test
    public void findTaskForInstanceId(){
        //获取流程引擎
        ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
        //TaskService
        TaskService taskService=processEngine.getTaskService();
        //得到当前实例下的所有任务
        List<Task> list = taskService.createTaskQuery()
                .processInstanceId("185001")
                .list();

        //遍历所有的 任务
        for (Task task : list) {
            System.out.println("任务id："+task.getId());
            System.out.println("责任人："+task.getAssignee());
            System.out.println("任务描述:"+task.getName());
        }

    }
}
